
import java.io.File;
import java.io.FileFilter;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JOptionPane;


public class FolderStats
{
  long folderCount = 0;
  long fileCount = 0;
  
  Set<String> tagNames = new HashSet<String>();
  Set<String> tagNamesLower = new HashSet<String>();
  
  String msg = "Folders... ";
  long lastFolderCount = 0;
  long diff = 100;
  
  FileFilter isFolder = new FileFilter(){
    @Override
    public boolean accept(File pathname)
    {
      return pathname.isDirectory();
    }
  };
  FileFilter isFile = new FileFilter(){
    @Override
    public boolean accept(File pathname)
    {
      return pathname.isFile();
    }
  };
  
  /**
   * @param args
   */
  public static void main(String[] args)
  {
    FolderStats t = new FolderStats();
        
    File root = new File("C:\\");
    
    t.folderStats(root);
    t.recurse(root);    
    
    System.out.println();
    System.out.println("--- " + root.getAbsolutePath() + " ---");
    System.out.println("files: " + t.fileCount);
    System.out.println("folders: " + t.folderCount);
    System.out.println("tags: " + t.tagNames.size());
    System.out.println("Case insensitive tags: " + t.tagNamesLower.size());
    
    JOptionPane.showMessageDialog(null, "Finished!");
  }
  
  void recurse(File f)
  {
    File[] files = f.listFiles(isFile);
    if (files != null)
      fileCount += files.length;
    
    File[] folders = f.listFiles(isFolder);
    if (folders == null)
      return;
    folderCount += folders.length;
    if (folderCount - lastFolderCount >= diff)
    {
      lastFolderCount = folderCount;
      System.out.println(msg + folderCount);
    }
    
    for (File d: folders)
    {
      tagNames.add(d.getName());
      tagNamesLower.add(d.getName().toLowerCase());
    }
    
    for (File d: folders)
    {
      recurse(d);
    }
  }
  
  
  
  public void folderStats(File f)
  {
    System.out.println("*** File system roots ***");
    File[] roots = File.listRoots();
    for (File r: roots)
      System.out.println(r.getAbsolutePath()); 
    System.out.println();
    
    System.out.println("Stats for: " + f.getAbsolutePath());
    try
    {
      System.out.println("Stats for: " + f.getCanonicalPath());
    }
    catch(Exception e)
    {}
    
    File[] files = f.listFiles(isFile);
    System.out.println();
    System.out.println("--- " + files.length + " files ---");
    for (File ff: files)
      System.out.println(ff.getName());
        
    File[] folders = f.listFiles(isFolder);
    System.out.println();
    System.out.println("--- " + folders.length + " folders ---");
    for (File ff: folders)
      System.out.println(ff.getName());
    
    File[] all = f.listFiles();
    System.out.println();
    System.out.println("--- " + all.length + " total ---");    
    for (File ff: all)
      System.out.println(ff.getName());
  }
}
